package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ppl.BallEliminate;
import com.yanqu.road.entity.activity.ppl.PplDrumReward;
import com.yanqu.road.entity.activity.ppl.PplLine;
import com.yanqu.road.entity.activity.ppl.PplShootResult;
import com.yanqu.road.entity.activity.ppl.PplUserData;
import com.yanqu.road.entity.activity.ppl.shootlogic.PplScreenPos;
import com.yanqu.road.pb.activity.PplProto;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 泡泡龙pb
 */
public class PplPb {

    /**
     * 发射球的结果 返回
     */
    public static PplProto.ShootBallRespMsg.Builder parseShootBallRespMsg(PplUserData userData, PplShootResult result) {
        PplProto.ShootBallRespMsg.Builder builder = PplProto.ShootBallRespMsg.newBuilder();
        builder.setRet(0);
        builder.setGameData(parsePplGameDataMsg(userData, userData.getCurMultiType()).build());
        builder.addAllBallList(parsePplEliminateBallMsgList(result.getEliminateList()));
        builder.addAllDrumRewardList(parsePplDrumRewardMsgList(result.getDrumRewardList()));
        return builder;
    }

    /**
     * 新发射球的结果 返回
     */
    public static PplProto.NewShootBallRespMsg.Builder parseNewShootBallRespMsg(PplUserData userData, PplShootResult result, List<PplScreenPos> moveKeyPos) {
        PplProto.NewShootBallRespMsg.Builder builder = PplProto.NewShootBallRespMsg.newBuilder();
        builder.setRet(0);
        builder.setGameData(parsePplGameDataMsg(userData, userData.getCurMultiType()).build());
        builder.addAllBallList(parsePplEliminateBallMsgList(result.getEliminateList()));
        builder.addAllDrumRewardList(parsePplDrumRewardMsgList(result.getDrumRewardList()));
        // 弹射点+被碰撞点+碰撞时自身的点+最终停留点
        PplScreenPos stopPos = moveKeyPos.remove(moveKeyPos.size() - 1);// 去掉停留点
        PplScreenPos collided = moveKeyPos.remove(moveKeyPos.size() - 2);// 去掉被碰撞点
        builder.addAllRouteList(parsePplScreenPosMsgList(moveKeyPos));
        builder.setStopPoint(parsePplArrayPosMsg(stopPos.getX(), stopPos.getY()));
        builder.setCollidedPoint(parsePplArrayPosMsg(collided.getX(), collided.getY()));
        builder.setTodayScore(userData.getTodayScore());
        return builder;
    }

    /**
     * 新发射球的结果 返回（飞出屏幕外专用）
     */
    public static PplProto.NewShootBallRespMsg.Builder parseNewShootBallOutScreenRespMsg(PplUserData userData, List<PplScreenPos> moveKeyPos) {
        PplProto.NewShootBallRespMsg.Builder builder = PplProto.NewShootBallRespMsg.newBuilder();
        builder.setRet(0);
        builder.addAllRouteList(PplPb.parsePplScreenPosMsgList(moveKeyPos));
        builder.setOutScreen(true);
        builder.setGameData(PplPb.parsePplGameDataMsg(userData, userData.getCurMultiType()));
        builder.setTodayScore(userData.getTodayScore());
        return builder;
    }

    /**
     * 泡泡龙小游戏数据
     *
     * @param multiType 1一倍，2四倍，3十六倍，4全部
     */
    public static PplProto.PplGameDataMsg.Builder parsePplGameDataMsg(PplUserData userData, int multiType) {
        PplProto.PplGameDataMsg.Builder gameDataMsg = PplProto.PplGameDataMsg.newBuilder();
        // 【1倍】
        gameDataMsg.setCurColor(userData.getCurColor1()); // required
        gameDataMsg.setGameOver(PplMgr.isGameOver(userData, 1)); // required
        if (multiType == 1 || multiType == 4) {
            List<PplProto.PplGameLineDataMsg> lineList = new ArrayList<>();
            for (PplLine pplLine : userData.getPplLineList1()) {
                PplProto.PplGameLineDataMsg.Builder lineBuilder = PplProto.PplGameLineDataMsg.newBuilder();
                lineBuilder.setY(pplLine.getY());
                lineBuilder.addAllColor(Arrays.stream(pplLine.getBallArray()).boxed().collect(Collectors.toList()));
                lineList.add(lineBuilder.build());
            }
            gameDataMsg.addAllLineList(lineList);
        }
        // 【4倍】
        if (multiType == 2 || multiType == 4) {
            gameDataMsg.setCurColor4(userData.getCurColor4());
            gameDataMsg.setGameOver4(PplMgr.isGameOver(userData, 2));
            List<PplProto.PplGameLineDataMsg> lineList = new ArrayList<>();
            for (PplLine pplLine : userData.getPplLineList4()) {
                PplProto.PplGameLineDataMsg.Builder lineBuilder = PplProto.PplGameLineDataMsg.newBuilder();
                lineBuilder.setY(pplLine.getY());
                lineBuilder.addAllColor(Arrays.stream(pplLine.getBallArray()).boxed().collect(Collectors.toList()));
                lineList.add(lineBuilder.build());
            }
            gameDataMsg.addAllLineList4(lineList);
        }
        // 【16倍】
        if (multiType == 3 || multiType == 4) {
            gameDataMsg.setCurColor16(userData.getCurColor16());
            gameDataMsg.setGameOver16(PplMgr.isGameOver(userData, 3));
            List<PplProto.PplGameLineDataMsg> lineList = new ArrayList<>();
            for (PplLine pplLine : userData.getPplLineList16()) {
                PplProto.PplGameLineDataMsg.Builder lineBuilder = PplProto.PplGameLineDataMsg.newBuilder();
                lineBuilder.setY(pplLine.getY());
                lineBuilder.addAllColor(Arrays.stream(pplLine.getBallArray()).boxed().collect(Collectors.toList()));
                lineList.add(lineBuilder.build());
            }
            gameDataMsg.addAllLineList16(lineList);
        }
        gameDataMsg.addAllDrumList(new ArrayList<>(userData.getDrumList()));
        gameDataMsg.setScore(userData.getScore());
        gameDataMsg.setMultiType(multiType);
        return gameDataMsg;
    }

    /**
     * 用户数据
     */
    public static PplProto.PplUserDataMsg.Builder parsePplUserDataMsg(PplUserData userData) {
        PplProto.PplUserDataMsg.Builder builder = PplProto.PplUserDataMsg.newBuilder();
        builder.setBeautyId(userData.getBeautyId());
        builder.setTodayScore(userData.getTodayScore());
        return builder;
    }

    /**
     * 消除掉的球
     */
    public static List<PplProto.PplEliminateBallMsg> parsePplEliminateBallMsgList(List<BallEliminate> eliminateList) {
        List<PplProto.PplEliminateBallMsg> eliminateBallMsgList = new ArrayList<>();
        for (BallEliminate ball : eliminateList) {
            PplProto.PplEliminateBallMsg.Builder eliminateBallMsg = PplProto.PplEliminateBallMsg.newBuilder();
            eliminateBallMsg.setX(ball.getX());
            eliminateBallMsg.setY(ball.getY());
            eliminateBallMsg.setDrumIndex(ball.getDrumIndex());
            if (!StringUtils.isNullOrEmpty(ball.getReward())) {
                eliminateBallMsg.setReward(ball.getReward());
            }
            eliminateBallMsgList.add(eliminateBallMsg.build());
        }
        return eliminateBallMsgList;
    }

    /**
     * 鼓集满奖励
     */
    public static List<PplProto.PplDrumRewardMsg> parsePplDrumRewardMsgList(List<PplDrumReward> drumRewardList) {
        List<PplProto.PplDrumRewardMsg> pplDrumRewardMsgList = new ArrayList<>();
        for (PplDrumReward drumReward : drumRewardList) {
            PplProto.PplDrumRewardMsg.Builder pplDrumRewardMsg = PplProto.PplDrumRewardMsg.newBuilder();
            pplDrumRewardMsg.setIndex(drumReward.getIndex());
            pplDrumRewardMsg.setReward(drumReward.getReward());
            pplDrumRewardMsgList.add(pplDrumRewardMsg.build());
        }
        return pplDrumRewardMsgList;
    }

    /**
     * 屏幕坐标
     */
    public static List<PplProto.PplScreenPosMsg> parsePplScreenPosMsgList(List<PplScreenPos> pplScreenPosList) {
        List<PplProto.PplScreenPosMsg> screenPosMsgList = new ArrayList<>();
        for (PplScreenPos screenPos : pplScreenPosList) {
            PplProto.PplScreenPosMsg.Builder builder = PplProto.PplScreenPosMsg.newBuilder();
            builder.setScreenX(screenPos.getScreenX().intValue());
            builder.setScreenY(screenPos.getScreenY().intValue());
            screenPosMsgList.add(builder.build());
        }
        return screenPosMsgList;
    }

    /**
     * 数组坐标
     */
    public static PplProto.PplArrayPosMsg parsePplArrayPosMsg(int x, int y) {
        PplProto.PplArrayPosMsg.Builder builder = PplProto.PplArrayPosMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        return builder.build();
    }
}
